package com.juyu.reader.pc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.juyu.reader.pc.common.Constants;
import com.juyu.reader.pc.model.Book;
import com.juyu.reader.pc.model.BookChapter;
import com.juyu.reader.pc.model.BookEx;
import com.juyu.reader.pc.model.Page;
import com.juyu.reader.pc.service.BookChapterService;
import com.juyu.reader.pc.service.BookExService;
import com.juyu.reader.pc.service.BookService;

@Controller
@RequestMapping("/home")
public class HomeController extends BaseController {
	Logger logger = Logger.getLogger(HomeController.class);
	@Autowired
	private BookExService bookExService;
	@Autowired
	private BookService bookService;
	@Autowired
	private BookChapterService bookChapterService;

	/**
	 * 首页跳转
	 * @param request
	 * @param response
	 * @param page
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response, Page page) {
		Map<String, Object> attrMap = new HashMap<String, Object>();
		this.setNavIndex(request, 1);
		/**
		 * 首页bananer t_book_ex_ad表
		 */
		BookEx findExAd = new BookEx();
		findExAd.setExAdPlatform(3);// 默认0，1H5平台，2移动平台，3PC平台
		findExAd.setExAdFlag(0);// 默认0，1男频，2女频，pc端暂时不设置男女频
		findExAd.setExAdType(1);//广告类型，默认0，1是首页广告（pc端的首页banner，移动端的首页banner），2是阅读首页广告，3pc横幅广告
		findExAd.setLimit(6);
		List<BookEx> bookExAdList = bookExService.findBookExAd(findExAd);
		// bananer广告位横幅
		attrMap.put("bookExAdList", bookExAdList);
		
		/**
		 * 广告位横幅（两幅） t_book_ex_ad表
		 */
		BookEx findExAdMid = new BookEx();
		findExAdMid.setExAdPlatform(3);// 默认0，1H5平台，2移动平台，3PC平台
		findExAdMid.setExAdFlag(0);// 默认0，1男频，2女频，pc端暂时不设置男女频
		findExAdMid.setExAdType(3);//广告类型，默认0，1是首页广告（pc端的首页banner，移动端的首页banner），2是阅读首页广告，3pc横幅广告
		findExAdMid.setLimit(2);//广告横幅只显示两条（长广告横幅）
		List<BookEx> bookExAdMidList = bookExService.findBookExAd(findExAdMid);
		// 广告位横幅（两幅）
		if (bookExAdMidList.size() >= 2) {
			attrMap.put("bookExAdMid1", bookExAdMidList.get(0));
			attrMap.put("bookExAdMid2", bookExAdMidList.get(1));
		}
		
		/**
		 * 热门推荐 t_book_ex_hot表
		 */
		BookEx findExHot = new BookEx();
		findExHot.setExHotPlatform(3);// 默认0，1H5平台，2移动平台，3PC平台
		findExHot.setExHotFlag(0);
		findExHot.setLimit(0);
		List<BookEx> bookExHotList = bookExService.findBookExHot(findExHot);
		// 热门推荐
		if (bookExHotList.size() >= 11) {
			//右上
			attrMap.put("bookExHot1", bookExHotList.get(0));
			//右下左
			attrMap.put("bookExHot2", bookExHotList.get(1));
			//左侧四个书籍封面
			attrMap.put("bookExHotLeftList", bookExHotList.subList(2, 6));
			//右下右5个书籍分类、名称
			attrMap.put("bookExHotRightList", bookExHotList.subList(6, 11));
		}
		
		/**
		 * 最新上架 t_book_ex_new表
		 */
		BookEx findExNew = new BookEx();
		findExNew.setExNewPlatform(3);// 默认0，1H5平台，2移动平台，3PC平台
		findExNew.setExNewFlag(0);
		findExNew.setLimit(0);
		List<BookEx> bookExNewList = bookExService.findBookExNew(findExNew);
		// 最新入库
		if (bookExNewList.size() >= 9) {
			//左
			attrMap.put("bookExNew1", bookExNewList.get(0));
			//右上
			attrMap.put("bookExNew4List", bookExNewList.subList(1, 5));
			//右下
			attrMap.put("bookExNew5", bookExNewList.get(5));
			attrMap.put("bookExNew6", bookExNewList.get(6));
			attrMap.put("bookExNew7", bookExNewList.get(7));
			attrMap.put("bookExNew8", bookExNewList.get(8));
		}
		
		/**
		 * 订阅排行 t_book_ex_pay表 只取前四条
		 */
		BookEx findExPay = new BookEx();
		findExPay.setExPayPlatform(3);// 默认0，1H5平台，2移动平台，3PC平台
		findExPay.setExPayFlag(0);
		findExPay.setLimit(0);
		List<BookEx> bookExPayList = bookExService.findBookExPay(findExPay);
		//订阅排行
		if(bookExPayList.size()>=4){
			attrMap.put("bookExPay1", bookExPayList.get(0));
			attrMap.put("bookExPay2", bookExPayList.get(1));
			attrMap.put("bookExPay3", bookExPayList.get(2));
			attrMap.put("bookExPay4", bookExPayList.get(3));
		}
		
		/**
		 * 主编力推 t_book_ex_editor表
		 */
		BookEx findExEditor = new BookEx();
		findExEditor.setExOffPlatform(3);// 默认0，1H5平台，2移动平台，3PC平台
		findExEditor.setExOffFlag(0);
		findExEditor.setLimit(10);
		List<BookEx> bookExEditorList = bookExService.findBookExEditor(findExEditor);
		// 主编力推
		if (bookExEditorList.size() >= 2) {
			attrMap.put("bookExEditor1", bookExEditorList.get(0));
			attrMap.put("bookExEditorList", bookExEditorList.subList(1, bookExEditorList.size()));
		}

		/**
		 * 排行榜 t_book表
		 */
		Book findBook = new Book();
		// 排行榜-点击
		findBook.setBookOrderBy(1);// 默认人气排序，1人气排序，2字数排序，3更新时间排序，4打赏排序
		findBook.setLimit(10);
		List<Book> bookClickList = bookService.findBookOrderBy(findBook);
		if (bookClickList.size() >= 10) {
			attrMap.put("bookClick1", bookClickList.get(0));
			attrMap.put("bookClickList", bookClickList.subList(1, 10));
		}
		// 排行榜-字数
		findBook.setBookOrderBy(2);// 默认人气排序，1人气排序，2字数排序，3更新时间排序，4打赏排序
		List<Book> bookWordsList = bookService.findBookOrderBy(findBook);
		if (bookWordsList.size() >= 10) {
			attrMap.put("bookWords1", bookWordsList.get(0));
			attrMap.put("bookWordsList", bookWordsList.subList(1, 10));
		}
		// 排行榜-更新时间
		findBook.setBookOrderBy(3);// 默认人气排序，1人气排序，2字数排序，3更新时间排序，4打赏排序
		List<Book> bookLatestList = bookService.findBookOrderBy(findBook);
		if (bookLatestList.size() >= 10) {
			attrMap.put("bookLatest1", bookLatestList.get(0));
			attrMap.put("bookLatestList", bookLatestList.subList(1, 10));
		}
		//打赏排行
		findBook.setBookOrderBy(4);// 默认人气排序，1人气排序，2字数排序，3更新时间排序，4打赏排序
		findBook.setLimit(4);//打赏排行只显示前4
		List<Book> bookRewardList = bookService.findBookOrderBy(findBook);
		attrMap.put("bookRewardList", bookRewardList);

		/**
		 * 经典完本 t_book表
		 */
		Book findFlagBook = new Book();
		findFlagBook.setBookFlag(1);// 0默认，0连载中，1已完结
		findFlagBook.setLimit(13);
		List<Book> bookFlagList = bookService.findBook(findFlagBook);
		if(bookFlagList.size()>=13){
			//左侧
			attrMap.put("bookFlag1", bookFlagList.get(0));
			//右上
			attrMap.put("bookFlag6List",  bookFlagList.subList(1, 7));
			//右下
			attrMap.put("bookFlag7", bookFlagList.get(7));
			attrMap.put("bookFlag8", bookFlagList.get(8));
			attrMap.put("bookFlag9", bookFlagList.get(9));
			attrMap.put("bookFlag10", bookFlagList.get(10));
			attrMap.put("bookFlag11", bookFlagList.get(11));
			attrMap.put("bookFlag12", bookFlagList.get(12));
		}

		/**
		 * 最近更新书籍 t_book表
		 */
		Book findLatestBook=new Book();
		findLatestBook.setLimit(20);
		findLatestBook.setBookOrderBy(3);// 默认人气排序，1人气排序，2字数排序，3更新时间排序
		List<Book> bookChapterLatestList = bookService.findBookOrderBy(findLatestBook);
        for(Book b:bookChapterLatestList){
        	List<BookChapter>latestChapters=bookChapterService.findBookChapterLatest(b.getBookId());
        	if(latestChapters.size()>=1){
        		b.setBookLatestChapterId(latestChapters.get(0).getChapterId());
        		b.setBookLatestChapter(latestChapters.get(0).getChapterName());
        	}
        }
        attrMap.put("bookChapterLatestList", bookChapterLatestList);
		/**
		 * 图片url前缀
		 */
		attrMap.put("sourceUrl", Constants.SOURCE_URL);
		return this.packModeAndView(request, "/book/index", null, attrMap);
		
//		Theme findTheme = new Theme();
//		findTheme.setThemePlatform(3);// 默认0，1H5平台，2移动平台，3PC平台
//		findTheme.setThemeFlag(0);
//		findTheme.setLimit(0);
//		List<Theme> themeList = themeService.findTheme(findTheme);
		
//		//半价
//		BookEx findExHalf = new BookEx();
//		findExHalf.setExHalfPlatform(3);// 默认0，1H5平台，2移动平台，3PC平台
//		findExHalf.setExHalfFlag(0);
//		findExHalf.setLimit(0);
//		List<BookEx> bookExHalfList = bookExService.findBookExHalf(findExHalf);
//		//半价
//		attrMap.put("bookExHalfList", bookExHalfList);
		
//		//今日限免		
//		BookEx findExFree = new BookEx();
//		findExFree.setExFreePlatform(3);// 默认0，1H5平台，2移动平台，3PC平台
//		findExFree.setExFreeFlag(0);
//		findExFree.setLimit(0);
//		List<BookEx> bookExFreeList = bookExService.findBookExFree(findExFree);
//		//今日限免
//		attrMap.put("bookExFreeList", bookExFreeList);
		
		//特价全本
//		BookEx findExOff = new BookEx();
//		findExOff.setExOffPlatform(3);// 默认0，1H5平台，2移动平台，3PC平台
//		findExOff.setExOffFlag(0);
//		findExOff.setLimit(5);
//		List<BookEx> bookExOffList = bookExService.findBookExOff(findExOff);
//		//特价全本
//		attrMap.put("bookExOffList", bookExOffList);
		
//		/** 图片url前缀*/
//		attrMap.put("sourceUrl", Constants.SOURCE_URL);
//		return this.packModeAndView(request, "/book/index", null, attrMap);
	}

}
